package com.ecommerce.dal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.ICompanyDAO;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;

@Repository("companyDAO")
public class CompanyDAO extends CustomHibernateDAOSupport implements
		ICompanyDAO {

	@Override
	public void save(Company company) {
		getHibernateTemplate().save(company);

	}

	@Override
	public void update(Company company) {
		getHibernateTemplate().update(company);

	}

	@Override
	public void delete(Company company) {
		getHibernateTemplate().delete(company);

	}

	@Override
	public Company findByCompanyId(long companyId) {
		List list = getHibernateTemplate().find(
				"from Company company where companyId=?", companyId);
		return (Company) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public List<Company> findAllCompanies() {
		List list = getHibernateTemplate().find("from Company");
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public Company findCompanyByItem(long itemId) {
		List list = getHibernateTemplate()
				.find("select company from Item item inner join item.client client inner join client.company company where item.itemId = ?",
						itemId);
		return (Company) list.get(0);
	}

}
